package cn.icatw.gulimall.order.web;

import cn.icatw.gulimall.order.config.AlipayTemplate;
import cn.icatw.gulimall.order.service.OrderService;
import cn.icatw.gulimall.order.vo.PayVo;
import com.alipay.api.AlipayApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 支付网络控制器
 *
 * @author icatw
 * @date 2022/3/30
 * @email 762188827@qq.com
 * @apiNote
 */
@Controller
@Slf4j
public class PayWebController {
    @Autowired
    AlipayTemplate alipayTemplate;
    @Autowired
    OrderService orderService;

    /**
     * 支付订单
     * 1、将支付页让浏览器显示
     * 2、支付成功以后 跳转到用户的订单列表页
     * @param orderSn 订单sn
     * @return {@link String}
     * @throws AlipayApiException 支付宝api例外
     */
    @ResponseBody
    @GetMapping(value = "/payOrder", produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {

        PayVo payVo = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        log.info(pay);
        return pay;
    }
}
